#!/usr/bin/env bash

file="/home/taoshanghaoke/service_status"
msg=""

# 内存
bu=`free | awk 'NR==2{print $6}'`
ca=`free | awk 'NR==2{print $7}'`
us=`free | awk 'NR==2{print $3}'`
to=`free | awk 'NR==2{print $2}'`
mem=`expr "scale=2;($us-$bu-$ca)/$to" |bc -l | cut -d. -f2`
if [[ $mem -gt 70 ]];then
    msg=${msg}"memory "
fi


# nginx 主进程是否存在
V_NGINX_NUM=`ps axu |grep 'nginx' |grep -v 'grep' |wc -l`
if [[ $V_NGINX_NUM -lt 1 ]];then
    msg=${msg}"nginx "
fi


# php-fpm 是否有进程存在
V_PHP_NUM=`ps axu | grep 'php-fpm' | grep -v 'grep' |wc -l`
if [[ $V_PHP_NUM -lt 1 ]];then
    msg=${msg}"php-fpm "
fi


# redis 是否有进程存在
V_REDIS_NUM=`ps axu | grep 'redis-server' | grep -v 'grep' |wc -l`
if [[ $V_REDIS_NUM -lt 1 ]];then
    msg=${msg}"redis "
fi


DISK_RATE=`df -h / | awk '{print $5}' | sed -n '2p'`
DISK_USED=${DISK_RATE/\%/}
if [[ $DISK_USED -gt 70 ]];then
    msg=${msg}" disk "
fi

if [[ ! -z $msg ]]; then
    `echo $msg > $file`
fi